package examples

import (
	"fmt"
	"gitee.com/Cauchy_AQ/gokv"
	"gitee.com/Cauchy_AQ/gokv/index"
	"time"
)

func ExamplesBasic() {
	opts := gokv.DefaultOptions
	opts.DirPath = "tmp"
	opts.DataFileSize = gokv.DataFile64M
	opts.DataFileMergeRatio = 0.25
	opts.SyncWrites = false
	opts.BytesSync = 0
	opts.TimeSync = time.Minute
	opts.IndexType = index.SKIP // index.ART、index.Btree、index.SKIP、index.BPTree
	opts.MMapAtStartUp = false

	var err error
	db, err := gokv.Open(&opts)
	if err != nil {
		panic(err)
	}

	err = db.Put([]byte("name"), []byte("cauchy"))
	if err != nil {
		panic(err)
	}

	value, err := db.Get([]byte("name"))
	if err != nil {
		panic(err)
	}
	fmt.Println(string(value))

	err = db.Delete([]byte("name"))
	if err != nil {
		panic(err)
	}
	_ = db.Put([]byte("version"), []byte("v0.1.0"))

	err = db.Merge()
	if err != nil {
		panic(err)
	}

	_ = db.Close()

	db, err = gokv.Open(&opts)
	defer db.Close()
	if err != nil {
		panic(err)
	}
	value, _ = db.Get([]byte("version"))
	fmt.Println(string(value))
}
